package com.xiaoyu.client.adapter.forum;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.forum.ForumDetailBean.DataBean.ForumReplayBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.forum.ForumDetailActivity;
import com.xiaoyu.client.ui.activity.forum.ForumFloatEditorActivity;
import com.xiaoyu.client.ui.activity.help.HelpDetailActivity;
import com.xiaoyu.client.ui.activity.help.UserSeekInfoActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.widget.NestedListView;
import com.xiaoyu.commonlib.utils.CCircleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jk on 2018/10/15.
 *
 * @Description 论坛一级回复列表适配器
 */
public class ForumReplyOneRecyAdapter extends RecyclerView.Adapter<ForumReplyOneRecyAdapter.ViewHolder> {
    public static final String FORUM_ID = "forum_reply_one_level_forumid";
    public static final String REPLY_ID = "forum_reply_one_level_replyid";
    public static final String USER_ID = "forum_reply_one_level_userid";
    private Context mContext;
    private List<ForumReplayBean> mList = new ArrayList<>();

    public ForumReplyOneRecyAdapter(Context mContext, List<ForumReplayBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_seek_reply_one_level, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        Glide.with(mContext).load(mList.get(position).getUserphoto()).into(holder.portraitImg);
        holder.nickNameTxt.setText(mList.get(position).getNickname());
        holder.contentTxt.setText(mList.get(position).getFourmreplaytitle());
        holder.timeTxt.setText(mList.get(position).getFourmreplaytime());
        holder.replyNumTxt.setText(mList.get(position).getReplaycount() == null ? "回复0" : "回复 " + mList.get(position).getReplaycount());

        if (mList.get(position).isLike()) {//已点赞
            holder.agreeNumTxt.setText("已赞 " + mList.get(position).getFourmreplaygree());
            holder.agreeNumTxt.setEnabled(false);
        } else {
            holder.agreeNumTxt.setText("点赞 " + mList.get(position).getFourmreplaygree());
            holder.agreeNumTxt.setEnabled(true);
        }


        if (position == getItemCount())//最后一条数据时，隐藏分割线
            holder.divider.setVisibility(View.GONE);
        else
            holder.divider.setVisibility(View.VISIBLE);

        if (mList.get(position).getChild().size() == 0 || mList.get(position).getChild() == null) {
            holder.replyLayout.setVisibility(View.GONE);
        } else {
            holder.replyLayout.setVisibility(View.VISIBLE);
        }
        ForumReplyTwoAdapter mAdapter = new ForumReplyTwoAdapter(mContext, mList.get(position).getChild());
        holder.replyLayout.setAdapter(mAdapter);

        holder.userLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, UserSeekInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(ClientConstants.FROM,ClientConstants.FROM_FORUM_DETAIL_ONE_REPLY);
                bundle.putString(USER_ID, mList.get(position).getUserid());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

        //点赞按钮
        holder.agreeNumTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toLike(holder, position);
            }
        });

        //回复按钮
        holder.replyNumTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ForumFloatEditorActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(ClientConstants.FROM, ClientConstants.FROM_FORUM_DETAIL);
                bundle.putString(FORUM_ID, mList.get(position).getForumid());
                bundle.putString(REPLY_ID, mList.get(position).getFourmreplayid());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout userLayout;
        private CCircleImageView portraitImg;
        private TextView nickNameTxt;
        private TextView contentTxt;
        private NestedListView replyLayout;
        private TextView timeTxt;
        private TextView replyNumTxt;
        private TextView agreeNumTxt;
        private View divider;

        public ViewHolder(View itemView) {
            super(itemView);
            userLayout = itemView.findViewById(R.id.item_seek_reply_one_level_user_layout);
            portraitImg = itemView.findViewById(R.id.item_seek_reply_one_level_portrait);
            nickNameTxt = itemView.findViewById(R.id.item_seek_reply_one_level_name_txt);
            contentTxt = itemView.findViewById(R.id.item_seek_reply_one_level_content_txt);
            replyLayout = itemView.findViewById(R.id.item_seek_reply_one_level_2_two_level_reply_layout);
            timeTxt = itemView.findViewById(R.id.item_seek_reply_one_level_publish_time_txt);
            replyNumTxt = itemView.findViewById(R.id.item_seek_reply_one_level_reply_num_txt);
            agreeNumTxt = itemView.findViewById(R.id.item_seek_reply_one_level_agree_num_txt);
            divider = itemView.findViewById(R.id.item_seek_reply_one_level_divider);
        }
    }

    private void toLike(final ViewHolder holder, final int position) {
        NetworkManager.likeForum(mList.get(position).getFourmreplayid(), false, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                int num = mList.get(position).getFourmreplaygree() + 1;
                holder.agreeNumTxt.setText("已赞 " + num);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                if (errorCode.equals("y402")) {
                    Toast.makeText(mContext, "已点赞", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }



}

